﻿
#include "customwidget.h"

EasyClickLabel::EasyClickLabel(QWidget* parent)
    :QLabel(parent)
{
    QFont contentFont=QFont("微软雅黑",10);
    this->setFont(contentFont);
    this->setStyleSheet("color:#365ac3");
}
void EasyClickLabel::mousePressEvent(QMouseEvent *event){
    this->setStyleSheet("color:#032790");
    MousePress=true;
}
void EasyClickLabel::mouseReleaseEvent(QMouseEvent *event){
    if(MousePress){
        this->setStyleSheet("color:#365ac3");
        emit this->clicked();
    }
    MousePress=false;

}

ClickLabel::ClickLabel(const QString &name, QWidget *parent):
    QWidget(parent)
{
    //设置字体
    QFont contentFont=QFont("Corbel",10);
    QFontMetrics fm(contentFont);
    qreal height=fm.lineSpacing();
    //qreal width=name.length();

    contentName=new QLabel(this);
    contentName->setText(name);
    contentName->setFont(contentFont);
    contentName->setFixedHeight(height);
    //contentName->setFixedWidth(width);
    contentName->setStyleSheet("color:#80baef");
    contentName->setAlignment(Qt::AlignVCenter);
    contentName->adjustSize();
    //qDebug()<<contentName->height()<<" ";
    //qDebug()<<contentName->width();

    this->setFixedHeight(contentName->height()+5);
    this->setFixedWidth(contentName->width()+5);
    contentName->move(margin,this->height() / 2 - contentName->height() / 2);
    //qDebug()<<this->height();
    //qDebug()<<this->width();
    bgWidget=new QWidget(this);
    bgWidget->setStyleSheet("background-color:#00000000;border-radius:10px;");
    bgWidget->setFixedHeight(this->height());
    bgWidget->setFixedWidth(this->width());
    bgWidget->lower();
    bgWidget->show();



    //bgOpac=new QGraphicsOpacityEffect(this);
    //bgOpac->setOpacity(0);

}
void ClickLabel::enterEvent(QEnterEvent *event){
    //修改字体颜色

    contentName->setStyleSheet("color:#f8f8f8");
    bgWidget->setStyleSheet("background-color:#298be5;border-radius:10px;");
}
void ClickLabel::leaveEvent(QEvent *event){
    contentName->setStyleSheet("color:#80baef");
    bgWidget->setStyleSheet("background-color:#00000000;border-radius:10px;");

}
void ClickLabel::mousePressEvent(QMouseEvent * event){
    bgWidget->setStyleSheet("background-color:#206cb2;border-radius:10px;");
    emit this->clicked();
    mousePressed=true;
}
void ClickLabel::mouseReleaseEvent(QMouseEvent *event){
    if(mousePressed){
         bgWidget->setStyleSheet("background-color:#298be5;border-radius:10px;");
    }
    mousePressed=false;

}
//***************//
textInputItem::textInputItem(const QString &name, QWidget *parent) :
    QWidget(parent)
{
    QFont nameFont = QFont("Microsoft YaHei", 14);
    QFontMetrics fm(nameFont);
    qreal height = fm.lineSpacing();
    itemName = new QLabel(this);
    itemName->setText(name);
    itemName->setFont(nameFont);
    itemName->setFixedHeight(height);
    itemName->setStyleSheet("color:#1c1c1c");

    QFont textFont = QFont("Microsoft YaHei", 14);
    fm = QFontMetrics(textFont);
    editor = new QLineEdit(this);
    editor->setText("");
    editor->setFixedHeight(fm.lineSpacing());
    editor->setStyleSheet("color:#5c5c5c;background-color:#00000000;border-style:none;");
    editor->setReadOnly(true);
    editor->setFont(textFont);

    bgWidget = new QWidget(this);
    bgWidget->setStyleSheet("background-color:#00000000;border-style:none;");
    bgWidget->lower();
    bgWidget->show();

    indicator = new QWidget(this);
    indicator->setFixedHeight(4);
    indicator->setStyleSheet("background-color:#0078d4;border-radius:2px");

    opac = new QGraphicsOpacityEffect(this);
    opac->setOpacity(0);
    indicator->setGraphicsEffect(opac);

    this->setFixedHeight(itemName->height() + 10);

    connect(editor, &QLineEdit::returnPressed, this, [=](){
        leaveEditEffect();
        onEditing = false;
        editor->setReadOnly(true);
        curText = editor->text();
    });
    connect(editor, &QLineEdit::editingFinished, this, [=](){
        leaveEditEffect();
        onEditing = false;
        editor->setReadOnly(true);
        curText = editor->text();
        QTimer *delay = new QTimer(this);
        connect(delay, &QTimer::timeout, this, [=](){mousePressed = false;});
        delay->setSingleShot(true);
        delay->start(10);
        mousePressed = false;
        emit textEdited(curText);
    });
}

void textInputItem::resizeEvent(QResizeEvent *event){
    itemName->move(margin, this->height() / 2 - itemName->height() / 2);
    itemName->setFixedWidth(this->width() * 0.3 - margin - spacing);
    int width = QFontMetrics(editor->font()).size(Qt::TextSingleLine, editor->text()).width() + 3;
    if(!onEditing){
        if(width > this->width() * 0.7 - margin)
            editor->resize(this->width() * 0.7 - margin, editor->height());
        else
            editor->resize(width, editor->height());
        editor->move(this->width() - margin - editor->width(), this->height() / 2 - editor->height() / 2);
        indicator->move(this->width() - margin, this->height() - 7);
    }
    else{
        editor->resize(this->width() * 0.7 - margin, editor->height());
        editor->move(this->width() * 0.3, this->height() / 2 - editor->height() / 2 - 2);
        indicator->move(this->width() * 0.3, this->height() - 7);
    }
    bgWidget->setFixedSize(this->size());
}

void textInputItem::enterEditEffect(){
    editor->setCursorPosition(editor->text().length());
    editor->setStyleSheet("color:#1c1c1c;background-color:#00000000;border-style:none;");
    QParallelAnimationGroup *group = new QParallelAnimationGroup(this);
    QPropertyAnimation *longer = new QPropertyAnimation(indicator, "geometry", this);
    longer->setStartValue(indicator->geometry());
    longer->setEndValue(QRectF(this->width() * 0.3, this->height() - 7, this->width() * 0.7 - margin, 4));
    longer->setDuration(500);
    longer->setEasingCurve(QEasingCurve::InOutExpo);
    QPropertyAnimation *fade = new QPropertyAnimation(opac, "opacity", this);
    fade->setStartValue(opac->opacity());
    fade->setEndValue(0.99);
    fade->setDuration(150);
    QPropertyAnimation *move = new QPropertyAnimation(editor, "geometry", this);
    move->setStartValue(editor->geometry());
    move->setEndValue(QRectF(this->width() * 0.3, this->height() / 2 - editor->height() / 2 - 2, this->width() * 0.7 - margin, editor->height()));
    move->setDuration(500);
    move->setEasingCurve(QEasingCurve::InOutExpo);
    group->addAnimation(longer);
    group->addAnimation(fade);
    group->addAnimation(move);
    group->start();
}

void textInputItem::leaveEditEffect(){
    editor->setCursorPosition(0);
    editor->setStyleSheet("color:#5c5c5c;background-color:#00000000;border-style:none;");
    QParallelAnimationGroup *group = new QParallelAnimationGroup(this);
    QPropertyAnimation *shorter = new QPropertyAnimation(indicator, "geometry", this);
    shorter->setStartValue(indicator->geometry());
    shorter->setEndValue(QRectF(this->width() - margin - 4, this->height() - 7, 4, 4));
    shorter->setDuration(500);
    shorter->setEasingCurve(QEasingCurve::InOutExpo);
    QPropertyAnimation *fade = new QPropertyAnimation(opac, "opacity", this);
    fade->setStartValue(opac->opacity());
    fade->setEndValue(0);
    fade->setDuration(350);
    QPropertyAnimation *move = new QPropertyAnimation(editor, "geometry", this);
    move->setStartValue(editor->geometry());
    int width = QFontMetrics(editor->font()).size(Qt::TextSingleLine, editor->text()).width() + 3;
    if(width > this->width() * 0.7 - margin)
        move->setEndValue(QRectF(this->width() * 0.3, this->height() / 2 - editor->height() / 2, this->width() * 0.7 - margin, editor->height()));
    else
        move->setEndValue(QRectF(this->width() - width - margin, this->height() / 2 - editor->height() / 2, width, editor->height()));
    move->setDuration(500);
    move->setEasingCurve(QEasingCurve::InOutExpo);
    group->addAnimation(shorter);
    group->addAnimation(fade);
    group->addAnimation(move);
    group->start();
}

void textInputItem::enterEvent(QEnterEvent *event){
    bgWidget->setStyleSheet("border-radius:5px;background-color:#0a000000");
}

void textInputItem::leaveEvent(QEvent *event){
    bgWidget->setStyleSheet("border-radius:5px;background-color:#00000000");
}

void textInputItem::mousePressEvent(QMouseEvent *event){
    bgWidget->setStyleSheet("border-radius:5px;background-color:#1a000000");
    mousePressed = true;
}

void textInputItem::mouseReleaseEvent(QMouseEvent *event){
    if(mousePressed){
        bgWidget->setStyleSheet("border-radius:5px;background-color:#0a000000");
        if(onEditing){
            leaveEditEffect();
            onEditing = false;
            curText = editor->text();
            editor->setReadOnly(true);
            emit textEdited(curText);
        }
        else{
            if(enabled){
                enterEditEffect();
                editor->raise();
                onEditing = true;
                editor->setReadOnly(false);
                editor->setText(curText + "");
                editor->setFocus();
            }
        }
        mousePressed = false;
    }
}

void textInputItem::setValue(const QString &text){
    editor->setText(text);
    editor->setCursorPosition(0);
    curText = text;
    int width = QFontMetrics(editor->font()).size(Qt::TextSingleLine, editor->text()).width() + 3;
    if(!onEditing){
        if(width > this->width() * 0.7 - margin)
            editor->resize(this->width() * 0.7 - margin, editor->height());
        else
            editor->resize(width, editor->height());
        editor->move(this->width() - margin - editor->width(), this->height() / 2 - editor->height() / 2);
    }
    else{
        editor->resize(this->width() * 0.7 - margin, editor->height());
        editor->move(this->width() * 0.3, this->height() / 2 - editor->height() / 2 - 2);
    }
}

messageInputItem::messageInputItem(const QString &name, QWidget *parent)
    :QWidget(parent)
{
    QFont nameFont = QFont("微软雅黑", 12);
    QFontMetrics fm(nameFont);
    qreal height = fm.lineSpacing();
    itemName = new QLabel(this);
    itemName->setText(name);
    itemName->setFont(nameFont);
    itemName->setFixedHeight(height);
    itemName->setStyleSheet("color:#1c1c1c");

    QFont textFont = QFont("Corbel", 12);
    fm = QFontMetrics(textFont);
    editor = new QLineEdit(this);
    editor->setText("");
    editor->setFixedHeight(fm.lineSpacing());
    editor->setStyleSheet("color:#5c5c5c;background-color:#00000000;border-style:none");
    editor->setReadOnly(true);
    editor->setFont(textFont);

    bgWidget = new QWidget(this);
    bgWidget->setStyleSheet("background-color:#0a000000;border-radius:5px;border: 1px solid #A0A0A0;");
    bgWidget->lower();
    bgWidget->show();

    indicator = new QWidget(this);
    indicator->resize(4,4);
    indicator->move(this->width()-margin,this->height()-7);
    indicator->setStyleSheet("background-color:#0078d4;border-radius:2px");
    opac = new QGraphicsOpacityEffect(this);
    opac->setOpacity(0);
    indicator->setGraphicsEffect(opac);

    connect(editor, &QLineEdit::returnPressed, this, [=](){
        leaveEditEffect();
        onEditing = false;
        editor->setReadOnly(true);
        curText = editor->text();
    });
    connect(editor, &QLineEdit::editingFinished, this, [=](){
        leaveEditEffect();
        onEditing = false;
        editor->setReadOnly(true);
        curText = editor->text();
        QTimer *delay = new QTimer(this);
        connect(delay, &QTimer::timeout, this, [=](){mousePressed = false;});
        delay->setSingleShot(true);
        delay->start(10);
        mousePressed = false;
        emit messageEdit(curText);
    });
}
void messageInputItem::resizeEvent(QResizeEvent *event){
    indicator->resize(this->width() * 0.1, 4);
    indicator->move(this->width()*0.45, this->height() -7);
    itemName->setFixedWidth(this->width() * 0.3 - margin - spacing);
    int width = QFontMetrics(editor->font()).size(Qt::TextSingleLine, editor->text()).width() + 3;
    if(!onEditing){
        if(width > this->width() * 0.7 - margin)
            editor->resize(this->width() * 0.7 - margin, editor->height());
        else
            editor->resize(width, editor->height());
        itemName->move(margin, this->height() / 2 - itemName->height() / 2);
        editor->move(this->width() - margin - editor->width(), this->height() / 2 - editor->height() / 2);

    }
    else{
        itemName->move(margin, this->height() / 2 - itemName->height() / 2-2);
        editor->resize(this->width() * 0.7 - margin, editor->height());
        editor->move(this->width() * 0.3, this->height() / 2 - editor->height() / 2 - 2);

    }
    bgWidget->setFixedSize(this->size());
}
void messageInputItem::enterEditEffect(){
    editor->setCursorPosition(editor->text().length());
    editor->setStyleSheet("color:#1c1c1c;background-color:#00000000;border-style:none;");
    QParallelAnimationGroup *group = new QParallelAnimationGroup(this);
    QPropertyAnimation *longer = new QPropertyAnimation(indicator, "geometry", this);
    longer->setStartValue(indicator->geometry());
    longer->setEndValue(QRectF(this->width()*0.1, this->height() - 7, this->width() * 0.8, 4));
    longer->setDuration(500);
    longer->setEasingCurve(QEasingCurve::OutBack);
    QPropertyAnimation *fade = new QPropertyAnimation(opac, "opacity", this);
    fade->setStartValue(opac->opacity());
    fade->setEndValue(0.99);
    fade->setDuration(150);
    QPropertyAnimation *move = new QPropertyAnimation(editor, "geometry", this);
    move->setStartValue(editor->geometry());
    move->setEndValue(QRectF(this->width() * 0.3, this->height() / 2 - editor->height() / 2 - 2, this->width() * 0.7 - margin, editor->height()));
    move->setDuration(500);
    move->setEasingCurve(QEasingCurve::InOutExpo);
    QPropertyAnimation * move2= new QPropertyAnimation(itemName, "geometry", this);
    move2->setStartValue(itemName->geometry());
    move2->setEndValue(QRectF(margin, this->height() / 2 - itemName->height() / 2 - 2, itemName->width(), itemName->height()));
    move2->setDuration(500);
    move2->setEasingCurve(QEasingCurve::InOutExpo);
    group->addAnimation(longer);
    group->addAnimation(fade);
    group->addAnimation(move);
    group->addAnimation(move2);
    group->start();
}

void messageInputItem::leaveEditEffect(){
    editor->setCursorPosition(0);
    editor->setStyleSheet("color:#5c5c5c;background-color:#00000000;border-style:none;");
    QParallelAnimationGroup *group = new QParallelAnimationGroup(this);
    QPropertyAnimation *shorter = new QPropertyAnimation(indicator, "geometry", this);
    shorter->setStartValue(indicator->geometry());
    shorter->setEndValue(QRectF(this->width() *0.45, this->height() - 7, this->width()*0.1, 4));
    shorter->setDuration(500);
    shorter->setEasingCurve(QEasingCurve::InOutExpo);
    QPropertyAnimation *fade = new QPropertyAnimation(opac, "opacity", this);
    fade->setStartValue(opac->opacity());
    fade->setEndValue(0);
    fade->setDuration(350);
    QPropertyAnimation *move = new QPropertyAnimation(editor, "geometry", this);
    move->setStartValue(editor->geometry());
    int width = QFontMetrics(editor->font()).size(Qt::TextSingleLine, editor->text()).width() + 3;
    if(width > this->width() * 0.7 - margin)
        move->setEndValue(QRectF(this->width() * 0.3, this->height() / 2 - editor->height() / 2, this->width() * 0.7 - margin, editor->height()));
    else
        move->setEndValue(QRectF(this->width() - width - margin, this->height() / 2 - editor->height() / 2, width, editor->height()));
    move->setDuration(500);
    move->setEasingCurve(QEasingCurve::InOutExpo);

    QPropertyAnimation *move2 = new QPropertyAnimation(itemName, "geometry", this);
    move2->setStartValue(itemName->geometry());
    move2->setEndValue(QRectF(margin, this->height() / 2 - itemName->height() / 2,itemName->width(),itemName->height()));
    move->setDuration(500);
    move->setEasingCurve(QEasingCurve::InOutExpo);
    group->addAnimation(shorter);
    group->addAnimation(fade);
    group->addAnimation(move);
    group->addAnimation(move2);
    group->start();
}

void messageInputItem::enterEvent(QEnterEvent *event){
    bgWidget->setStyleSheet("border-radius:7px;background-color:#1a000000;border: 1px solid #298DFF;");
}

void messageInputItem::leaveEvent(QEvent *event){
    bgWidget->setStyleSheet("border-radius:5px;background-color:#0a000000;border: 1px solid #A0A0A0;");
}
void messageInputItem::mousePressEvent(QMouseEvent *event){
    bgWidget->setStyleSheet("border-radius:7px;background-color:#2a000000;border: 1px solid #298DFF;");
    mousePressed = true;
}

void messageInputItem::mouseReleaseEvent(QMouseEvent *event){
    if(mousePressed){
        bgWidget->setStyleSheet("border-radius:7px;background-color:#0a000000;border: 1px solid #298DFF;");
        if(onEditing){
            leaveEditEffect();
            onEditing = false;
            curText = editor->text();
            editor->setReadOnly(true);
            emit messageEdit(curText);
        }
        else{
            if(enabled){
                enterEditEffect();
                editor->raise();
                onEditing = true;
                editor->setReadOnly(false);
                editor->setText(curText + "");
                editor->setFocus();
            }
        }
        mousePressed = false;
    }
}
void messageInputItem::setValue(const QString &text){
    editor->setText(text);
    editor->setCursorPosition(0);
    curText = text;
    int width = QFontMetrics(editor->font()).size(Qt::TextSingleLine, editor->text()).width() + 3;
    if(!onEditing){
        if(width > this->width() * 0.7 - margin)
            editor->resize(this->width() * 0.7 - margin, editor->height());
        else
            editor->resize(width, editor->height());
        editor->move(this->width() - margin - editor->width(), this->height() / 2 - editor->height() / 2);
    }
    else{
        editor->resize(this->width() * 0.7 - margin, editor->height());
        editor->move(this->width() * 0.3, this->height() / 2 - editor->height() / 2 - 2);
    }
}
